package com.yuong.demo.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.yuong.demo.DensityUtil;

import androidx.annotation.Nullable;

/**
 * 〈分层圆圈〉
 *
 * @author yuandong
 * @create 2021/5/7
 * @since 1.0.0
 */
public class LayeredCircleView extends View {
    private int mWidth, mHeight;
    private float mMaxCircleWidth;
    private float centerX, centerY;
    private float circleStrokeWidth;
    private float circleInterval;
    private float lineStrokeWidth;
    private float textSize;
    private int paddingTop, paddingBottom;

    private Paint circlePaint;
    private Paint arcPaint;
    private Paint textPaint;
    private Paint linePaint;
    private Paint.FontMetrics textFontMetrics;
    private int[] angles = {20, 35, 60, 85, 180};
    private String[] values = {"大运游泳馆   2311", "大运体育馆   1788", "南山综合馆   1200", "贵港综合馆   1200", "柳州综合馆   1200"};

    public LayeredCircleView(Context context) {
        this(context, null);
    }

    public LayeredCircleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LayeredCircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        circleStrokeWidth = DensityUtil.dp2px(getContext(), 6);
        circleInterval = DensityUtil.dp2px(getContext(), 6);
        lineStrokeWidth = DensityUtil.dp2px(getContext(), 0.5f);
        textSize = DensityUtil.sp2px(getContext(), 12);
        paddingTop = (int) DensityUtil.dp2px(getContext(), 20);
        paddingBottom = (int) DensityUtil.dp2px(getContext(), 20);

        circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        circlePaint.setStyle(Paint.Style.STROKE);
        circlePaint.setStrokeWidth(circleStrokeWidth);
        circlePaint.setColor(Color.parseColor("#ECECEC"));

        arcPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        arcPaint.setStyle(Paint.Style.STROKE);
        arcPaint.setStrokeWidth(circleStrokeWidth);
        arcPaint.setStrokeCap(Paint.Cap.ROUND);
        arcPaint.setColor(Color.parseColor("#B592FF"));

        linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        linePaint.setStyle(Paint.Style.FILL);
        linePaint.setColor(Color.BLACK);
        linePaint.setStrokeWidth(lineStrokeWidth);

        textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setStyle(Paint.Style.FILL);
        textPaint.setColor(Color.parseColor("#000000"));
        textPaint.setTextSize(textSize);
        textPaint.setTextAlign(Paint.Align.RIGHT);
        textFontMetrics = textPaint.getFontMetrics();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = MeasureSpec.getSize(widthMeasureSpec);
        mMaxCircleWidth = 0.45f * mWidth;
        mHeight = (int) (paddingTop + paddingBottom + mMaxCircleWidth);
        centerY = paddingTop + mMaxCircleWidth * 0.5f;
        centerX = mMaxCircleWidth * 0.5f;
        setMeasuredDimension(mWidth, mHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //画圆环
        drawCircle(canvas);
        //画圆弧
        drawArc(canvas);
        //线和文字
        drawLineAndText(canvas);
    }

    private void drawCircle(Canvas canvas) {
        for (int i = 0; i < 5; i++) {
            float radius = mMaxCircleWidth * 0.5f - i * circleInterval - (2 * i + 1) * circleStrokeWidth * 0.5f;
            canvas.drawCircle(centerX, centerY, radius, circlePaint);
        }
    }

    private void drawArc(Canvas canvas) {
        canvas.rotate(-90, centerX, centerY);//以圆心旋转的
        for (int i = 0; i < 5; i++) {
            float radius = mMaxCircleWidth * 0.5f - i * circleInterval - (2 * i + 1) * circleStrokeWidth * 0.5f;

            float left = centerX - radius;
            float right = centerX + radius;
            float top = centerY - radius;
            float bottom = centerY + radius;

            RectF oval = new RectF(left, top, right, bottom);
            canvas.drawArc(oval, 0, 180, false, arcPaint);
        }
        canvas.rotate(90, centerX, centerY);
    }

    private void drawLineAndText(Canvas canvas) {
        for (int i = 0; i < 5; i++) {
            float radius = mMaxCircleWidth * 0.5f - i * circleInterval - (2 * i + 1) * circleStrokeWidth * 0.5f;
            int angle = angles[i];
            String text = values[i];
            double pi = Math.PI * angle / 180d;
            float x = (float) (centerX + Math.sin(pi) * radius);
            float y = (float) (centerY - Math.cos(pi) * radius);
            canvas.drawLine(x, y, mWidth, y, linePaint);
            textPaint.measureText(text);
            canvas.drawText(text, mWidth, y - textFontMetrics.bottom, textPaint);
        }
    }
}